package com.txsj.scan.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.Button;

import com.google.gson.Gson;
import com.txsj.scan.R;
import com.txsj.scan.adapter.WareHouseAdapter;
import com.txsj.scan.base.BaseActivity;
import com.txsj.scan.bean.WarehouseBean;
import com.txsj.scan.presenter.SelectWarehousePresenter;
import com.txsj.scan.refresh.XRefreshView;
import com.txsj.scan.view.IWarehouseView;
import com.txsj.scan.widget.swipemenu.SwipeRecyView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by wangchuangjun on 2019/2/18
 * Desc: 选择仓库列表页
 */

public class SelectWarehouseActivity extends BaseActivity<SelectWarehousePresenter> implements IWarehouseView {

    private List<WarehouseBean> warehouseBeanList;
    WareHouseAdapter adapter;
    XRefreshView xRefreshView;
    SwipeRecyView recyclerView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_select_warehouse);
        initView();
        initData();

    }

    private void initData() {
        this.warehouseBeanList = new ArrayList<>();
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        adapter = new WareHouseAdapter(this);
        recyclerView.setAdapter(adapter);
        adapter.setListener(new WareHouseAdapter.OnItemClickListener() {
            @Override
            public void onCLick(int position) {
                Intent intent = getIntent();
                String json = new Gson().toJson(warehouseBeanList.get(position));
                intent.putExtra("result",json);
                setResult(0x185,intent);
                finish();
            }
        });
        initEmptyView();
        xRefreshView.enableEmptyView(false);
        xRefreshView.setPullRefreshEnable(true);
        xRefreshView.setAutoLoadMore(false);
        xRefreshView.startRefresh();
        xRefreshView.setXRefreshViewListener(new XRefreshView.SimpleXRefreshListener(){
            @Override
            public void onRefresh(boolean isPullDown) {
                super.onRefresh(isPullDown);
                presenter.getWarehouseHttp();
            }
        });

    }

    private void initView() {
        recyclerView = (SwipeRecyView) findViewById(R.id.recyclerView);
        xRefreshView = (XRefreshView) findViewById(R.id.xRefreshView);

    }

    @Override
    protected SelectWarehousePresenter providePresenter() {
        return presenter =new SelectWarehousePresenter(this);
    }

    @Override
    public Context context() {
        return this;
    }

    @Override
    public void onError(String msg) {
        hideLoading();
        xRefreshView.stopRefresh();
        showErrorMsg(msg);
    }

    @Override
    public void onSuccess(List<WarehouseBean> list) {
        hideLoading();
        xRefreshView.stopRefresh();
        warehouseBeanList.clear();
        warehouseBeanList.addAll(list);
        adapter.setList(list);
    }

    @Override
    public void onLoading() {
        showLoading();
    }

    Button btn_empty_reload;

    /**
     * 列表数据为空时 显示的View
     */
    private void initEmptyView() {
        View emptyView = View.inflate(this, R.layout.view_empty, null);
        btn_empty_reload = (Button) emptyView.findViewById(R.id.btn_empty_reload);
        xRefreshView.setEmptyView(emptyView);
        btn_empty_reload.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                xRefreshView.startRefresh();
            }
        });
    }
}
